/* 
 * Ext GWT 2.2.5 - Ext for GWT 
 * Copyright(c) 2007-2010, Ext JS, LLC. 
 * licensing@extjs.com 
 *  
 * http://extjs.com/license 
 */

package sk.gunna.sms.client;

  
import java.util.ArrayList;  
import java.util.Date;
import java.util.List;  

import sk.gunna.sms.client.user.UserService;
import sk.gunna.sms.client.user.UserServiceAsync;
import sk.gunna.sms.shared.UserBean;
   
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;  
import com.extjs.gxt.ui.client.data.ModelData;  
import com.extjs.gxt.ui.client.event.ButtonEvent;  
import com.extjs.gxt.ui.client.event.SelectionListener;  
import com.extjs.gxt.ui.client.store.ListStore;  
import com.extjs.gxt.ui.client.util.DateWrapper;  
import com.extjs.gxt.ui.client.widget.ContentPanel;  
import com.extjs.gxt.ui.client.widget.LayoutContainer;  
import com.extjs.gxt.ui.client.widget.button.Button;  
import com.extjs.gxt.ui.client.widget.form.CheckBox;  
import com.extjs.gxt.ui.client.widget.form.DateField;  
import com.extjs.gxt.ui.client.widget.form.NumberField;  
import com.extjs.gxt.ui.client.widget.form.SimpleComboBox;  
import com.extjs.gxt.ui.client.widget.form.TextField;  
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;  
import com.extjs.gxt.ui.client.widget.grid.CellEditor;  
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;  
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;  
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;  
import com.extjs.gxt.ui.client.widget.grid.Grid;  
import com.extjs.gxt.ui.client.widget.grid.RowEditor;  
import com.extjs.gxt.ui.client.widget.layout.FitLayout;  
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;  
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;  
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;  
import com.google.gwt.i18n.client.NumberFormat;  
import com.google.gwt.user.client.Element;  
import com.google.gwt.user.client.rpc.AsyncCallback;


public class RowEditorExample extends LayoutContainer {  
  
	private final UserServiceAsync userService = GWT.create(UserService.class);
	
  @Override  
  protected void onRender(Element parent, int index) {  
    super.onRender(parent, index);  
    setLayout(new FlowLayout(10));  
  
    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
  
    ColumnConfig column = new ColumnConfig();  
    column.setId("email");  
    column.setHeader("Email");  
    column.setWidth(220);  
  
    TextField<String> text = new TextField<String>();  
    text.setAllowBlank(false);  
    column.setEditor(new CellEditor(text));  
    configs.add(column);  
  
    final SimpleComboBox<String> combo = new SimpleComboBox<String>();  
    combo.setForceSelection(true);  
    combo.setTriggerAction(TriggerAction.ALL);  
    combo.add("Reader");  
    combo.add("Admin");  
    combo.add("SuperAdmin");  
  
    CellEditor editor = new CellEditor(combo) {  
      @Override  
      public Object preProcessValue(Object value) {  
        if (value == null) {  
          return value;  
        }  
        return combo.findModel(value.toString());  
      }  
  
      @Override  
      public Object postProcessValue(Object value) {  
        if (value == null) {  
          return value;  
        }  
        return ((ModelData) value).get("value");  
      }  
    };  
  
    column = new ColumnConfig();  
    column.setId("userType");  
    column.setHeader("Typ uctu");  
    column.setWidth(130);  
    column.setEditor(editor);  
    configs.add(column);  
  
    column = new ColumnConfig();  
    column.setId("creationDate");  
    column.setHeader("Vytvorene");  
    //column.setAlignment(HorizontalAlignment.RIGHT);  
    column.setWidth(95);  
    //column.setNumberFormat(NumberFormat.getCurrencyFormat());  
    //column.setEditor(new CellEditor(new NumberField())); 
    column.setDateTimeFormat(DateTimeFormat.getFormat("MMM dd yyyy"));  
  
    configs.add(column);  
  
    DateField dateField = new DateField();  
    //dateField.getPropertyEditor().setFormat(DateTimeFormat.getFormat("MM/dd/y"));  
  
    column = new ColumnConfig();  
    column.setId("lastLogin");  
    column.setHeader("Posledny login");  
    column.setWidth(95);  
    //column.setEditor(new CellEditor(dateField));  
    column.setDateTimeFormat(DateTimeFormat.getFormat("MMM dd yyyy"));  
    configs.add(column);  
  /*
    CheckColumnConfig checkColumn = new CheckColumnConfig("indoor", "Indoor?", 55);  
    CellEditor checkBoxEditor = new CellEditor(new CheckBox());  
    checkColumn.setEditor(checkBoxEditor);  
    configs.add(checkColumn);  
  */
    
    final ListStore<UserBean> store = new ListStore<UserBean>();  
    
    userService.getUsers(new AsyncCallback<List<UserBean>>() {
		public void onFailure(Throwable error) {
		}
		public void onSuccess(List<UserBean> users) {
			store.add(users);
		}
	});
    
   
    ColumnModel cm = new ColumnModel(configs);  
  
    ContentPanel cp = new ContentPanel();  
    //cp.setIcon(Resources.ICONS.table());  
    cp.setHeading("Edit Plants with RowEditor");  
    cp.setFrame(true);  
    cp.setSize(600, 300);  
    cp.setLayout(new FitLayout());  
  
    final RowEditor<UserBean> re = new RowEditor<UserBean>();  
    final Grid<UserBean> grid = new Grid<UserBean>(store, cm);  
    grid.setAutoExpandColumn("email");  
    grid.setBorders(true);  
    //grid.addPlugin(checkColumn);  
    grid.addPlugin(re);  
    grid.getAriaSupport().setLabelledBy(cp.getHeader().getId() + "-label");  
    cp.add(grid);  
  
    ToolBar toolBar = new ToolBar();  
    Button add = new Button("Pridaj pouzivatela");  
    add.addSelectionListener(new SelectionListener<ButtonEvent>() {  
  
      @Override  
      public void componentSelected(ButtonEvent ce) {  
    	UserBean user = new UserBean();  
    	user.setUserEmail("email");  
    	user.setUserType("Admin");  
    	user.setCreateDate(new Date());  
    	
        re.stopEditing(false);  
        store.insert(user, 0);  
        re.startEditing(store.indexOf(user), true);  
  
      }  
  
    });  
    toolBar.add(add);  
    cp.setTopComponent(toolBar);  
    cp.setButtonAlign(HorizontalAlignment.CENTER);  
    cp.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
  
      @Override  
      public void componentSelected(ButtonEvent ce) {  
        store.rejectChanges();  
      }  
    }));  
  
    cp.addButton(new Button("Save", new SelectionListener<ButtonEvent>() {  
  
      @Override  
      public void componentSelected(ButtonEvent ce) {  
        store.commitChanges();  
      }  
    }));  
  
    add(cp);  
  }  
  
} 